﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using LibKetoan;
using System.Data;

namespace Ketoan
{
	/// <summary>
	/// Summary description for frmSaochep.
	/// </summary>
	public class frmSaochep : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ComboBox cboKysl;
		private System.Windows.Forms.Label label18;
		private System.Windows.Forms.Button butKetthuc;
		private System.Windows.Forms.Button butcapnhat;
		private AccessData a;
		private int i_kysl,i_userid;
		private string s_yyyy;
		private string s_yy;
		private DataTable dtKysl=new DataTable();
		private DataTable dtkc=new DataTable();
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmSaochep(AccessData acc,int kysl,string yyyy, string yy,int userid)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			a=acc;
			i_kysl=kysl;
			s_yyyy=yyyy;
			s_yy=yy;
			i_userid=userid;
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmSaochep));
			this.cboKysl = new System.Windows.Forms.ComboBox();
			this.label18 = new System.Windows.Forms.Label();
			this.butKetthuc = new System.Windows.Forms.Button();
			this.butcapnhat = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// cboKysl
			// 
			this.cboKysl.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboKysl.Location = new System.Drawing.Point(100, 8);
			this.cboKysl.Name = "cboKysl";
			this.cboKysl.Size = new System.Drawing.Size(208, 21);
			this.cboKysl.TabIndex = 142;
			// 
			// label18
			// 
			this.label18.Location = new System.Drawing.Point(20, 8);
			this.label18.Name = "label18";
			this.label18.Size = new System.Drawing.Size(64, 16);
			this.label18.TabIndex = 143;
			this.label18.Text = "Kỳ số liệu:";
			// 
			// butKetthuc
			// 
			this.butKetthuc.Image = ((System.Drawing.Bitmap)(resources.GetObject("butKetthuc.Image")));
			this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butKetthuc.Location = new System.Drawing.Point(165, 40);
			this.butKetthuc.Name = "butKetthuc";
			this.butKetthuc.Size = new System.Drawing.Size(70, 25);
			this.butKetthuc.TabIndex = 154;
			this.butKetthuc.Text = "&Kết thúc";
			this.butKetthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
			// 
			// butcapnhat
			// 
			this.butcapnhat.Image = ((System.Drawing.Bitmap)(resources.GetObject("butcapnhat.Image")));
			this.butcapnhat.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butcapnhat.Location = new System.Drawing.Point(93, 40);
			this.butcapnhat.Name = "butcapnhat";
			this.butcapnhat.Size = new System.Drawing.Size(72, 25);
			this.butcapnhat.TabIndex = 153;
			this.butcapnhat.Text = " &Cập nhật";
			this.butcapnhat.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butcapnhat.Click += new System.EventHandler(this.butcapnhat_Click);
			// 
			// frmSaochep
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(336, 77);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.butKetthuc,
																		  this.butcapnhat,
																		  this.cboKysl,
																		  this.label18});
			this.MaximizeBox = false;
			this.Name = "frmSaochep";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "frmSaochep";
			this.Load += new System.EventHandler(this.frmSaochep_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void frmSaochep_Load(object sender, System.EventArgs e)
		{
			cboKysl.ValueMember="MA";	
			cboKysl.DisplayMember="TEN";
			load_cboKysl();
		}
		private void load_cboKysl()
		{
			dtKysl=a.get_data("select id,ten||' Năm "+s_yyyy+"' as ten,ma from "+a.user+".a_dmkybc where id<>"+i_kysl+" order by id").Tables[0];
			cboKysl.DataSource=dtKysl;			
		}

		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void butcapnhat_Click(object sender, System.EventArgs e)
		{
			long l_id=0;
			dtkc=a.get_data("select * from "+a.user+".a_ctkc"+s_yy+" where kysl="+i_kysl+"").Tables[0];
			foreach(DataRow r in dtkc.Rows)
			{				
				try
				{
					l_id=int.Parse(a.get_data("select max(id) from "+a.user+".a_ctkc"+s_yy+"").Tables[0].Rows[0][0].ToString())+1;
				}
				catch{l_id=1;}				
				//kiem tra mau ket chuyen dang chon da co trong ctkc chua?
				string sql="select * from "+a.user+".a_ctkc"+s_yy+" where sobtoan like '"+r["sobtoan"].ToString().Trim()+"' and kysl="+Convert.ToInt32(cboKysl.SelectedValue.ToString())+"";
				if(a.get_data(sql).Tables[0].Rows.Count==0)						
					a.upd_ctkc(int.Parse(l_id.ToString()),decimal.Parse(l_id.ToString()),s_yy,int.Parse(r["ketchuyen"].ToString().Trim()),
						r["sobtoan"].ToString().Trim(),r["tenbt"].ToString().Trim(),int.Parse(r["tinhchat"].ToString().Trim()),
						int.Parse(r["tutk"].ToString().Trim()),int.Parse(r["sangtk"].ToString().Trim()),r["kmcpnguon"].ToString().Trim(),
						r["nhomkmcpn"].ToString().Trim(),r["dtthnguon"].ToString().Trim(),int.Parse(r["tukmcpn"].ToString().Trim()),
						r["kmcpnguon2"].ToString().Trim(),r["kmcpnguon3"].ToString().Trim(),
						r["kmcpnguon4"].ToString().Trim(),r["kmcpdich"].ToString().Trim(),
						r["kmcpdich2"].ToString().Trim(),r["kmcpdich3"].ToString().Trim(),
						r["kmcpdich4"].ToString().Trim(),int.Parse(r["tudtthn"].ToString().Trim()),
						r["dtthdich"].ToString().Trim(),Convert.ToInt32(cboKysl.SelectedValue.ToString()),r["lknguon"].ToString().Trim(),
						int.Parse(r["tulkn"].ToString().Trim()),r["lkdich"].ToString().Trim(),i_userid,DateTime.Now);														
			}
			MessageBox.Show("Đã cập nhật!");
		}
	}
}
